//
//  ICMCustomerAndCarViewController.m
//  ICrEAM
//
//  Created by Cty on 2016/11/06.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMCustomerAndCarViewController.h"
#import "CustomerAndCar.h"
@interface ICMCustomerAndCarViewController ()<UIScrollViewDelegate>

@property (nonatomic,strong) UIScrollView *container;
@property (nonatomic,strong) UIButton *leftButton;
@property (nonatomic,strong) UIButton *rightButton;
@property (nonatomic,strong) UIButton *ownerInfoButton;
@property (nonatomic,strong) UIButton *carInfoButton;
@property (nonatomic,strong) UIView *titleContainer;

@property (assign) NSInteger index;
@end

@implementation ICMCustomerAndCarViewController
- (UIScrollView *)container{
    if(!_container){
        _container = [[UIScrollView alloc]initWithFrame:CGRectMake(ActualWidth(78), ActualHeight(70), ScreenWidth-2*ActualWidth(78), ScreenHeight-ActualHeight(70)-NavBarAndStatusHeight)];
        
        _container.contentSize = CGSizeMake(2*(ScreenWidth-2*ActualWidth(78)), ScreenHeight-ActualHeight(70)-NavBarAndStatusHeight);
        _container.bounces = NO;
        _container.pagingEnabled = YES;
        _container.delegate = self;
        _container.showsHorizontalScrollIndicator = NO;
        _container.showsVerticalScrollIndicator = NO;
    }
    return _container;
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    self.personerModel.adviserName = self.carModel.adviserName;
    self.personerModel.vwOneHome = self.carModel.HQ_VW_CARD_ID;
    self.personerModel.oneHomeNum = self.carModel.HQ_VW_CARD_ID;
    self.super_VIN = self.carModel.vinNum;
    self.super_owner_no = self.personerModel.OWNER_NO;
    
    CustomerAndCar *customerView = [CustomerAndCar creatViewWithFrame:CGRectMake(0, 0, ScreenWidth-2*ActualWidth(78), ScreenHeight-ActualHeight(70)-NavBarAndStatusHeight) andPersonModel:self.personerModel];
    
    CustomerAndCar *carView = [CustomerAndCar creatViewWithFrame:CGRectMake(ScreenWidth-2*ActualWidth(78), 0, ScreenWidth-2*ActualWidth(78), ScreenHeight-ActualHeight(70)-NavBarAndStatusHeight) andCarModel:self.carModel];
    
    [self.container addSubview:customerView];
    [self.container addSubview:carView];
    
    self.view.backgroundColor = BackgroundColor_White;
    [self.view addSubview:self.container];
    
    //上一页
    self.leftButton = [[UIButton alloc]initWithFrame:CGRectMake(ActualWidth(78)/2-ActualWidth(15), ScreenHeight/2-ActualHeight(30)-NavBarAndStatusHeight/2, ActualWidth(30), ActualHeight(30))];
    [self.leftButton setTitle:@"<" forState:UIControlStateNormal];
    self.leftButton.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.leftButton addTarget:self action:@selector(clickLeft) forControlEvents:UIControlEventTouchUpInside];
    self.leftButton.titleLabel.textColor = Color(0xA2A2A2);
    [self.leftButton setTitleColor:Color(0xA2A2A2) forState:UIControlStateNormal];
    //下一页
    self.rightButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-ActualWidth(78)/2-ActualWidth(15), ScreenHeight/2-ActualHeight(30)-NavBarAndStatusHeight/2, ActualWidth(30), ActualHeight(30))];
    [self.rightButton setTitle:@">" forState:UIControlStateNormal];
    self.rightButton.titleLabel.font = [UIFont systemFontOfSize:30];
    [self.rightButton addTarget:self action:@selector(clickRight) forControlEvents:UIControlEventTouchUpInside];
    [self.rightButton setTitleColor:Color(0xA2A2A2) forState:UIControlStateNormal];
    self.leftButton.hidden = YES;
    [self.view addSubview:self.leftButton];
    [self.view addSubview:self.rightButton];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
    //Title栏
    self.titleContainer = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ActualHeight(70))];
    //车主信息按钮
    self.ownerInfoButton = [[UIButton alloc]initWithFrame:CGRectMake((ScreenWidth-ActualWidth(150))/2, 0, ActualWidth(150), ActualHeight(70))];
    
    
    [self.ownerInfoButton setUpButtonWithNormolTitle:@"车主信息" selectedTitle:@"车主信息"  normolTitleSize: ActualWidth(16) selectedTitleSize:ActualWidth(16) normolTitleColor:Color(0x9A9A9A) selectedTitleColor: TitleColor normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(clickOwnerBtn:) target:self];
    
    [self.titleContainer addSubview:self.ownerInfoButton];
    
    //车辆信息按钮
    self.carInfoButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth-ActualWidth(150), 0, ActualWidth(150), ActualHeight(70))];
    [self.carInfoButton setUpButtonWithNormolTitle:@"车辆信息" selectedTitle:@"车辆信息"  normolTitleSize: ActualWidth(16) selectedTitleSize:ActualWidth(16) normolTitleColor:Color(0x9A9A9A) selectedTitleColor: TitleColor normolBcakImage:nil selectedBackImage:nil actionSEL:@selector(clickCarBtn:) target:self];
    
    [self.titleContainer addSubview:self.carInfoButton];
    
    if(self.entrance == 0)
        [self setOwnerSelected];
    else
        [self setCarSelected];
    [self.view addSubview:self.titleContainer];
    
}

-(void)clickOwnerBtn:(UIButton*)sender{
    
    if(![sender isSelected]){
        
        [self setOwnerSelected];
        
    }
}
-(void)clickCarBtn:(UIButton*)sender{
    
    if(![sender isSelected]){
        
        [self setCarSelected];
        
    }
    
}
-(void)titleBarOffsetToOwner{
//    [UIView animateWithDuration:0.2 animations:^{
        self.titleContainer.centerX = ScreenWidth/2;
//    }];
}
-(void)titleBarOffsetToCar{
//    [UIView animateWithDuration:0.2 animations:^{
        self.titleContainer.centerX = ActualWidth(150)/2;
//    }];
}
-(void)setOwnerSelected{
    [self.ownerInfoButton setSelected:YES];
    [self.carInfoButton setSelected:NO];
    
    self.ownerInfoButton.titleLabel.font = [UIFont systemFontOfSize:ActualWidth(20)];
    
    self.carInfoButton.titleLabel.font = [UIFont systemFontOfSize:ActualWidth(15)];
    [self clickLeft];
    [self titleBarOffsetToOwner];
}
-(void)setCarSelected{
    [self.carInfoButton setSelected:YES];
    [self.ownerInfoButton setSelected:NO];
    
    
    self.ownerInfoButton.titleLabel.font = [UIFont systemFontOfSize:ActualWidth(15)];
    
    self.carInfoButton.titleLabel.font = [UIFont systemFontOfSize:ActualWidth(20)];
    
    [self clickRight];
    
    [self titleBarOffsetToCar];
}
-(void)clickLeft{
    if (self.index == 0) {
        return;
    }
    self.index--;
    self.leftButton.hidden = YES;
    self.rightButton.hidden = NO;
    [self.container scrollRectToVisible:CGRectMake((ScreenWidth-2*ActualWidth(78))*self.index, 0, ScreenWidth-2*ActualWidth(78), ScreenHeight-ActualHeight(70)-NavBarAndStatusHeight) animated:YES];
}

-(void)clickRight{
    if (self.index == 1) {
        return;
    }
    self.index++;
    self.leftButton.hidden = NO;
    self.rightButton.hidden = YES;
    [self.container scrollRectToVisible:CGRectMake((ScreenWidth-2*ActualWidth(78))*self.index, 0, ScreenWidth-2*ActualWidth(78), ScreenHeight-ActualHeight(70)-NavBarAndStatusHeight) animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //float offsetX = scrollView.contentOffset.x;
    self.index = roundf(scrollView.contentOffset.x/(ScreenWidth-ActualWidth(140)));
    
    if(self.index == 0){
        self.leftButton.hidden = YES;
        self.rightButton.hidden = NO;
        [self setOwnerSelected];
    }else{
        self.leftButton.hidden = NO;
        self.rightButton.hidden = YES;
        [self setCarSelected];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
    //    float offsetX = scrollView.contentOffset.x;
    self.index = roundf(scrollView.contentOffset.x/(ScreenWidth-ActualWidth(140)));
    
    if(self.index == 0){
        self.leftButton.hidden = YES;
        self.rightButton.hidden = NO;
        [self setOwnerSelected];
    }else{
        self.leftButton.hidden = NO;
        self.rightButton.hidden = YES;
        [self setCarSelected];
    }
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
